package org.example.diary.exceptions;

import lombok.Getter;
import org.example.diary.enums.BizErrorCode;
import org.springframework.boot.logging.LogLevel;

/**
 * 系统异常封装
 */
@Getter
public class SystemException extends RuntimeException {

    /**
     * 错误码
     */
    private int code;

    /**
     * 错误消息
     */
    private String message;

    /**
     * 日志级别
     */
    private LogLevel logLevel = LogLevel.ERROR;


    protected SystemException(Exception e) {
        super(e);
        this.code = BizErrorCode.INTERNAL_ERROR.getCode();
        this.message = e.getMessage();
    }

    public SystemException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public SystemException(SystemErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMessage());
    }

    protected SystemException(int code, String message, LogLevel logLevel) {
        this(code, message);
        this.logLevel = logLevel;
    }

    protected SystemException(SystemErrorCode errorCode, LogLevel logLevel) {
        this(errorCode.getCode(), errorCode.getMessage(), logLevel);
    }


    public static SystemException of(int code, String message) {
        return new SystemException(code, message);
    }

    public static SystemException of(SystemErrorCode errorCode) {
        return new SystemException(errorCode);
    }

    public static SystemException of(SystemErrorCode errorCode, LogLevel logLevel) {
        return new SystemException(errorCode, logLevel);
    }

    /**
     * 抛出系统异常
     *
     * @param errorCode 错误码
     */
    public static void throwing(SystemErrorCode errorCode) {
        throw new SystemException(errorCode);
    }

    /**
     * 抛出系统异常，按照指定的日志级别打印日志
     *
     * @param errorCode 错误码
     * @param logLevel  日志级别
     */
    public static void throwing(SystemErrorCode errorCode, LogLevel logLevel) {
        throw new SystemException(errorCode, logLevel);
    }

    /**
     * 抛出系统异常
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static void throwing(SystemErrorCode errorCode, String message) {
        throw SystemException.of(errorCode.getCode(), message);
    }


    /**
     * 抛出系统异常
     *
     * @param code    错误码
     * @param message 错误信息
     */
    public static void throwing(int code, String message) {
        throw SystemException.of(code, message);
    }

    /**
     * 抛出系统异常，按照指定的日志级别打印日志
     */
    public static void throwing(int code, String message, LogLevel logLevel) {
        throw new SystemException(code, message, logLevel);
    }

}
